<template>
  <div class="main-section-menu">
    <div class="search-block">
      <!-- <div class="form-group">
        <input type="text" placeholder="学号搜索">
      </div> -->
      <el-select v-model="studentIDValue"
        remote
        placeholder="学号搜索" filterable size="large" class="form-group" clearable
        :remote-method="handleStudentIDSearch"
        @change="handleStudentIDChange"
        >
        <el-option v-for="(item, index) in studentIDList" :key="index" :label="item" :value="item" />
      </el-select>

      <el-select v-model="studentNameValue"
        remote
        placeholder="姓名搜索" filterable size="large" class="form-group" clearable
        :remote-method="handleStudentNameSearch"
        @change="handleStudentNameChange"
        >
        <el-option v-for="(item, index) in studentNameList" :key="index" :label="item" :value="item" />
      </el-select>

      <el-select v-model="majorValue" placeholder="专业搜索" filterable size="large" class="form-group" clearable
        :filter-method="filterMajorSearch" @change="handleMajorChange">
        <el-option v-for="(item, index) in outMajorList" :key="index" :label="item" :value="item" />
      </el-select>

      <el-select v-model="gradeValue" placeholder="年级搜索" filterable size="large" class="form-group" clearable
        :filter-method="filterGradeSearch" @change="handleGradeChange">
        <el-option v-for="(item, index) in outGradeList" :key="index" :label="item" :value="item" />
      </el-select>

      <div class="form-group-bar"> </div>
      <div class="college-name" :class="{ active: collegeNameIndex == 1 }" @click="handleFacultyChange(1, $event)">
        容艺影视产业学院</div>
      <div class="college-name" :class="{ active: collegeNameIndex == 2 }" @click="handleFacultyChange(2, $event)">
        数据产业学院
      </div>
      <div class="college-name" :class="{ active: collegeNameIndex == 3 }" @click="handleFacultyChange(3, $event)">
        数字金融产业学院</div>
      <div class="college-name" :class="{ active: collegeNameIndex == 4 }" @click="handleFacultyChange(4, $event)">
        游戏产业学院
      </div>
      <div class="college-name" :class="{ active: collegeNameIndex == 5 }" @click="handleFacultyChange(5, $event)">
        智能建造产业学院</div>
      <div class="college-name" :class="{ active: collegeNameIndex == 6 }" @click="handleFacultyChange(6, $event)">
        智能产业学院
      </div>
      <div class="college-name" :class="{ active: collegeNameIndex == 7 }" @click="handleFacultyChange(7, $event)">
        现代通信产业学院</div>
      <div class="college-name" :class="{ active: collegeNameIndex == 8 }" @click="handleFacultyChange(8, $event)">
        未来技术产业学院</div>
    </div>
  </div>
  <div class="main-section-display" v-if="!loading">
    <el-carousel ref="carousel" class="wrap" height="624px" direction="vertical" :autoplay="isAutoplay"
      v-if="imageFileTokens.length" indicator-position="none" @change="fullScreenIndex = -1">
      <el-carousel-item v-for="(itemArr, i) in imageFileTokens" :key="i">
        <div class="flex flex-wrap align-start">
          <div class="image-block" :class="{ active: index === fullScreenIndex }"
            @click="handleShowFullScreenDisplay(index)" v-for="(item, index) in itemArr" :key="index">
            <img :src="item.image_url">
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>

    <div class="carousel-control">
      <div class="carousel-control_up" @click="carousel.prev()">
        <img src="@/assets/images/arrow.png" alt="" srcset="">
      </div>
      <div class="carousel-control_middle"></div>
      <div class="carousel-control_down" @click="carousel.next()">
        <img src="@/assets/images/arrow.png" alt="" srcset="">
      </div>
    </div>
  </div>

  <div class="main-section-display"
    v-loading="loading"
    :element-loading-svg="svg"
    element-loading-svg-view-box="-10, -10, 50, 50" v-else>
  </div>
</template>

<script setup>
import { ImageSearch, GetStudentStyleMeta, GetStudentsById, GetStudentsByName } from "@/api";
import { onMounted } from "vue";

import { ref } from "vue";

const carousel = ref(null);
const majorList = ref([]);
const outMajorList = ref([]);
const majorValue = ref('')
const gradeList = ref([]);
const outGradeList = ref([]);
const gradeValue = ref('')
const imageFileTokens = ref([]);

const fullScreenIndex = ref(-1);
const collegeNameIndex = ref(-1);

const defaultFileTokens = ref([]);

const studentIDList = ref([]);
const studentIDValue = ref('')

const studentNameList = ref([]);
const studentNameValue = ref('')


const isAutoplay = ref(true)

const loading = ref(false)
const svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `

onMounted(async () => {
  const { data } = await GetStudentStyleMeta();

  majorList.value = data.majors
  outMajorList.value = data.majors

  gradeList.value = data.years
  outGradeList.value = data.years

  loadDefaultData()
});

function loadDefaultData() {
  ImageSearch({
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;
    defaultFileTokens.value = fileTokens;
  });
}

function resetSearchCond() {
}

function filterMajorSearch(value) {
  outMajorList.value = majorList.value.filter(item => {
    return item.toLowerCase().includes(value.toLowerCase());
  });
}

function filterGradeSearch(value) {
  outGradeList.value = gradeList.value.filter(item => {
    return item.toLowerCase().includes(value.toLowerCase());
  });
}

function splitArrayIntoChunks(data, chunkSize = 24) {
  const result = [];
  const totalLength = data.length;

  for (let i = 0; i < totalLength; i += chunkSize) {
    const start = i;
    const end = i + chunkSize;
    result.push(data.slice(start, end));
  }

  return result;
}

function handleStudentIDSearch(query) {
  if(query) {
    GetStudentsById(query).then(res => {
      studentIDList.value = res.data.map(e => e.student_id)
    })
  }
}

function handleStudentIDChange(value) {
  loading.value = true

  gradeValue.value = ""
  majorValue.value = ""
  studentNameValue.value = ""

  collegeNameIndex.value = -1;
  carousel.value.setActiveItem(0) // 重置归零
  imageFileTokens.value = [];
  if (value === '') {
    return;
  }
  resetSearchCond()
  ImageSearch({
    student_id: value,
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;
  }).finally(() => {
    setTimeout(() => {
      loading.value = false
    }, 800)
  })
}

function handleStudentNameSearch(query) {
  if(query) {
    GetStudentsByName(query).then(res => {
      studentNameList.value = res.data.map(e => e.student_name)
    })
  }
}

function handleStudentNameChange(value) {
  loading.value = true

  gradeValue.value = ""
  studentIDValue.value = ""
  majorValue.value = ""

  collegeNameIndex.value = -1;
  carousel.value.setActiveItem(0) // 重置归零
  imageFileTokens.value = [];
  if (value === '') {
    return;
  }
  resetSearchCond()
  ImageSearch({
    student_name: value,
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;
  }).finally(() => {
    setTimeout(() => {
      loading.value = false
    }, 800)
  })
}


function handleMajorChange(value) {
  loading.value = true

  gradeValue.value = ""
  studentIDValue.value = ""
  studentNameValue.value = ""

  collegeNameIndex.value = -1;
  carousel.value.setActiveItem(0) // 重置归零
  imageFileTokens.value = [];
  if (value === '') {
    return;
  }
  resetSearchCond()
  ImageSearch({
    major: value,
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;
  }).finally(() => {
    setTimeout(() => {
      loading.value = false
    }, 800)
  })
}

function handleGradeChange(value) {
  loading.value = true

  majorValue.value = ""
  studentIDValue.value = ""
  studentNameValue.value = ""

  collegeNameIndex.value = -1;
  carousel.value.setActiveItem(0) // 重置归零
  imageFileTokens.value = [];
  if (value === '') {
    return;
  }
  resetSearchCond()
  ImageSearch({
    year: value,
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;
  }).finally(() => {
    setTimeout(() => {
      loading.value = false
    }, 800)
  })
}

function handleFacultyChange(index, event) {
  majorValue.value = ""
  gradeValue.value = ""
  studentIDValue.value = ""
  studentNameValue.value = ""

  fullScreenIndex.value = -1;
  isAutoplay.value = true

  collegeNameIndex.value = index;
  loading.value = true
  carousel.value.setActiveItem(0) // 重置归零
  const facultyName = event.target.innerText
  ImageSearch({
    college: facultyName,
    image_type: "学生风采"
  }).then(res => {
    const fileTokens = splitArrayIntoChunks(res.data);
    imageFileTokens.value = fileTokens;

    console.log(res)
  }).finally(() => {
    setTimeout(() => {
      loading.value = false
    }, 800)
  })
}

function handleShowFullScreenDisplay(index) {
  if (index === fullScreenIndex.value) {
    fullScreenIndex.value = -1;
    isAutoplay.value = true
  } else {
    fullScreenIndex.value = index;
    isAutoplay.value = false
  }
}
</script>

<style lang="css" scoped></style>


<style scoped lang="scss">
.search-block {
  padding-top: 27px;
}

.form-group {
  width: auto;
  margin: 0 20px;
  border: 5px solid #703B32;
  display: block;
  height: 45px;
  line-height: 1;
  border-radius: 45px;
  overflow: hidden;
  margin-bottom: 12px;

  ::v-deep &>div {
    height: 45px;
    line-height: 1;
    box-shadow: none !important;
  }

  ::v-deep .el-select__placeholder {
    text-align: center;
  }
}

input {
  width: 100%;
  height: 100%;
  outline: none;
  text-align: center;
  border: none;
}

.form-group-bar {
  margin: 0 20px;
  width: auto;
  height: 5px;
  background-color: #E76C00;
  margin-bottom: 12px;
}

.college-name {
  margin: 0 20px;
  width: auto;
  height: 35px;
  line-height: 35px;
  border-radius: 45px;
  overflow: hidden;
  margin-bottom: 12px;
  text-align: center;
  background-color: #E76C00;
  color: #fff;

  &.active {
    background-color: #28a745;
  }
}

.wrap {
  width: 940px;
  height: 624px;
  background-color: #fff;
  margin-top: 37.5px;
  margin-left: 35px;
}

.image-block {
  width: 150px;
  height: 150px;
  background-color: rgba(0, 0, 0, .95);
  margin-right: 8px;
  margin-bottom: 8px;
  overflow: hidden;
  text-align: center;
  cursor: pointer;
  position: relative;

  &.active {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 999;
  }

  &:nth-child(6n) {
    margin-right: 0;
  }

  img {
    height: 100%;
  }
}

.carousel-control {
  position: absolute;
  top: 50%;
  right: 30px;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;

  &_up {
    width: 50px;
    height: 50px;
    cursor: pointer;
    margin-bottom: 20px;
  }

  &_middle {
    width: 50px;
    height: 50px;
    border: 5px solid #E76C00;
    margin-bottom: 20px;
    border-radius: 5px;
  }

  &_down {
    width: 50px;
    height: 50px;
    cursor: pointer;
    transform: rotate(180deg);
  }
}
</style>
